﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccessLayer;
using System.IO;
using SavoryBL;

namespace WebApplication
{
    public partial class _Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
            }
            ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divbreadcrum")).InnerHtml = @"<a href='default.aspx'>Home</a> > <span>Sign In / Sign Up</span>";
        }
        public void btnSignIn_Click(object sender, EventArgs e)
        {
            if (btnSignIn.Text == "Sign In" && txtPassword.Text.Trim() != "Password")
            {

                SavoryUserDetails obj = new SavoryUserDetails();
                string userName = txtUserName.Text.Trim();
                string password = txtPassword.Text.Trim();
                if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password))
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "Please enter valid User Name and Password ";
                }
                else
                {
                    UesrList user = clsUser.IsAuthenticated(userName, password);
                    if (null != user)
                    {
                        Session["ID"] = user.UserID;
                        Session["UserID"] = user.FirstName + " " + user.LastName;
                        Session["IsMember"] = user.IsMembership;
                        if((Request["url"] != null) && (Request["url"] != "/Sign-In.aspx"))
                        {
                            Response.Redirect(Request["url"].Replace('@','&'));
                        }
                        else
                        {
                            if (user.IsMembership == "True")
                            {
                                if (obj.IsMemberApprovedByAdmin(userName,password))
                                {
                                    Response.Redirect("Menu.aspx");
                                }
                                else
                                {
                                    lblMessage.Visible = true;
                                    lblMessage.Text = "Sorry !! Your account is not approved by Admin.";
                                }
                            }
                            else
                            {
                                Response.Redirect("Menu.aspx");
                            }
                        }

                    }
                    else
                    {
                        lblMessage.Visible = true;
                        lblMessage.Text = "Please enter valid User Name and Password ";
                    }
                }
            }
            else
            {
                string userName = txtUserName.Text.Trim();
                if (string.IsNullOrEmpty(userName))
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "Please enter valid Email ID";
                }
                else
                {
                    UesrList user = clsUser.ForgetPassword(userName);
                    if (null != user)
                    {
                       string MailContent =  File.ReadAllText(Server.MapPath("ForgetPassword.aspx"));
                       MailContent = MailContent.Replace("%%%%%%", userName);
                       MailContent  = MailContent.Replace("######", user.Password);
                       Utility.sendMail(userName, "Forget Password - Savory Housing", MailContent);
                        Response.Redirect("Thanks.aspx");
                    }
                    else
                    {
                        lblMessage.Visible = true;
                        lblMessage.Text = "You are not registered user. Please check your Email ID.";
                    }
                }
            }
        }
        public void btnSignUp_Click(object sender, EventArgs e)
        {
            if (chkNonMemberShip.Checked)
            {
                Response.Redirect("NonMemberShip.aspx");
            }
            else
            {
                Response.Redirect("Membership.aspx");
            }
        }
        
    }
}
